Searching Files Linux-cheat-sheet

🧾 Linux grep & find Cheat Sheet

πŸ” grep β€” Search Inside Files

Command Example Explanation
grep "word" grep "error" log.txt Find lines in log.txt containing the word "error".
grep -i "word" grep -i "error" log.txt Case-insensitive search for "error" in log.txt.
grep -w "word" grep -w "fail" logs.txt Match only the whole word "fail", not "failed" or "failure".
grep -x "line" grep -x "OK" status.txt Match lines that are exactly "OK".
grep -v "word" grep -v "error" log.txt Show lines that don’t contain "error".
grep -n "text" grep -n "fail" logs.txt Show matching lines with their line numbers.
grep -c "text" grep -c "404" web.log Count how many lines contain "404".
grep -l "word" grep -l "root" *.conf Show filenames (only) that contain "root".
grep -o "pattern" grep -o "user[0-9]*" users.txt Show only the matching part (like user123).
grep "^text" grep "^n" file.txt Find lines starting with "n".
grep -i "^net" grep -i "^net" config.txt Case-insensitive lines starting with "net".
grep "text$" grep "\.conf$" filenames.txt Find lines ending with .conf.
grep -A 2 "error" grep -A 2 "error" syslog Show 2 lines after each "error".
grep -B 2 "error" grep -B 2 "error" syslog Show 2 lines before each "error".
grep -C 2 "error" grep -C 2 "error" syslog Show 2 lines before & after each match.
grep -r "text" grep -r "TODO" . Recursively search "TODO" in all files under current dir.
`grep -E "A B"` `grep -E "cat
grep -F "1.2.3.4" grep -F "1.2.3.4" ip.txt Match exact string, no regex used.

πŸ”Ž find β€” Search for Files and Directories

Command Example Explanation
find . -name "file" find . -name "notes.txt" Search for notes.txt in current directory and subdirs.
find /path -iname "file" find /etc -iname "host.conf" Case-insensitive search for host.conf in /etc.
find . -type f find . -type f Find all files under current directory.
find . -type d find . -type d Find all directories under current directory.
find . -name "*.log" find . -name "*.log" Find all .log files under current directory.
find . -size +10M find . -size +10M Find files larger than 10MB.
find . -size -100k find . -size -100k Find files smaller than 100KB.
find . -empty find . -empty Find all empty files and directories.
find /var -mtime -1 find /var -mtime -1 Files modified in the last 1 day.
find . -user USERNAME find . -user root Find files owned by a specific user.
find . -perm 777 find . -perm 777 Find files or folders with 777 permission.
find . -exec COMMAND {} \; find . -name "*.tmp" -exec rm {} \; Find and delete all .tmp files.
find . -newer file.txt find . -newer backup.txt Find files modified more recently than backup.txt.

βœ… Tips:

  • Use grep when you're looking inside files.
  • Use find when you're looking for files themselves.

Would you like this saved as a downloadable .md file or copied to your Obsidian vault?